
#include "main.h"
#include "octree.h"

lua_State* L = 0;

extern Octree octree;

static int c_width = 640, c_height = 480;
static bool c_fullscreen = false;

int config_width() { return c_width; }
int config_height() { return c_height; }
int config_fullscreen() { return c_fullscreen; }

int l_videomode(lua_State* L)
{
	c_width  = luaL_checkint(L,1);
	c_height = luaL_checkint(L,2);
	if (lua_type(L,3) != LUA_TNONE)
	{
		luaL_checktype(L, 3, LUA_TBOOLEAN);
		c_fullscreen = lua_toboolean(L,3);
	}
	return 0;
}

bool luaL_checkboolean(lua_State* L, int param)
{
	luaL_checktype(L, param, LUA_TBOOLEAN);
	return (bool) lua_toboolean(L, param);
}

void script_init()
{
	L = luaL_newstate();
	if (!L) die("Lua failed to initialize!");

	luaL_openlibs(L);

	lua_register(L, "VideoMode", l_videomode);
	lua_register(L, "Box", l_Box);
	lua_register(L, "Spider", l_Spider);
	lua_register(L, "Camera_SetPosition", l_Camera_SetPosition);
	lua_register(L, "Camera_Move", l_Camera_Move);
	lua_register(L, "Camera_MoveRelative", l_Camera_MoveRelative);
	lua_register(L, "Camera_SetRotation", l_Camera_SetRotation);
	lua_register(L, "Camera_Rotate", l_Camera_Rotate);
	lua_register(L, "Camera_GetRotation", l_Camera_GetRotation);
	lua_register(L, "Spider_Move", l_Spider_Move);
	lua_register(L, "Spider_MoveLeg", l_Spider_MoveLeg);
	lua_register(L, "Spider_GetPos", l_Spider_GetPos);
	lua_register(L, "Spider_GetLegPos", l_Spider_GetLegPos);
	lua_register(L, "Spider_SetLegMode", l_Spider_SetLegMode);
	lua_register(L, "Spider_GetLegMode", l_Spider_GetLegMode);
	lua_register(L, "Sound_Tak", l_Sound_Tak);
//	lua_register(L, "setvolume", l_setvolume);
//	lua_register(L, "printoctree", l_printoctree);

	if (luaL_dofile(L, "data/main.lua"))
		die("%s",lua_tostring(L,1));

}

void script_fini()
{
	lua_close(L);
}

